package cn.smile.interceptor.home;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import cn.smile.entity.common.SiteSetting;
import cn.smile.service.common.FriendLinkService;
import cn.smile.service.common.GoodsCategoryService;
import cn.smile.service.common.SiteSettingService;
import cn.smile.util.StringUtil;

/**
 * 前台全局拦截器
 */
@Component
public class HomeGlobalInterceptor implements HandlerInterceptor {

    @Resource
    private GoodsCategoryService goodsCategoryService;
    @Resource
    private FriendLinkService friendLinkService;
    @Resource
    private SiteSettingService siteSettingService;

    @SuppressWarnings("SpellCheckingInspection")
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        if (!StringUtil.isAjax(request)) {
            //若不是ajax请求，则将菜单信息放入页面模板变量
            request.setAttribute("goodsCategorys", goodsCategoryService.findAll());
            request.setAttribute("friendLinkList", friendLinkService.findList(8));
            SiteSetting siteSetting = siteSettingService.find();
            if (siteSetting != null) {
                request.setAttribute("siteName", siteSetting.getSiteName());
                request.setAttribute("siteSetting", siteSetting);
            }
        }
        return true;
    }
}
